<?php
/**
 * Created by PhpStorm.
 * User: shaoxia
 * Date: 19-12-6
 * Time: 上午3:27
 */

namespace zwx\daemon\console;
use yii\console\Controller;

abstract class BaseDaemonController extends Controller
{
    private $running = true;

    //信号处理函数
    public function signalHandler($signo)
    {
        echo "" . getmypid() . " signalHandler($signo)\n";
        switch ($signo) {
            //中断进程
            case SIGTERM:
            case SIGHUP:
            case SIGQUIT:
                $this->running = false;
                break;
            default:
                break;
        }
    }

    public function actionRun($params)
    {
        pcntl_signal(SIGTERM, array($this, "signalHandler"), false);
        pcntl_signal(SIGINT, array($this, "signalHandler"), false);
        pcntl_signal(SIGQUIT, array($this, "signalHandler"), false);

        while ($this->running) {
            $this->perform($params);
            usleep(100000);
            pcntl_signal_dispatch();
        }
    }

    abstract protected function perform($params);
}